package com.hskn.hss.module.tlopetation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tloperationitem.mapper.TlOperationItemMapper;
import com.hskn.hss.module.tlopetation.entity.TlOperation;
import com.hskn.hss.module.tlopetation.mapper.TlOperationMapper;
import com.hskn.hss.module.tlopetation.service.ITlOperationService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-15
 */
@Service
public class TlOperationServiceImpl extends ServiceImpl<TlOperationMapper, TlOperation> implements ITlOperationService {

    @Resource
    TlOperationMapper tlOperationMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;
    @Resource
    TlOperationItemMapper tlOperationItemMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlOperation tlOperation = params.getObject("data", TlOperation.class);

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlOperation.setCreateby(uid);
        tlOperation.setCreatetime(new Date());
        tlOperation.setDeptid(deptid);
        if (tlOperation.getStatus() == null || tlOperation.getStatus().isEmpty()) {
            tlOperation.setStatus("1");
        }
        tlOperationMapper.insert(tlOperation);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlOperation tlOperation = params.getObject("data", TlOperation.class);
        tlOperation.setUpdateby(uid);
        tlOperation.setUpdatetime(new Date());
        tlOperationMapper.updateById(tlOperation);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlOperationMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlOperation getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlOperation tlOperation = tlOperationMapper.selectById(id);
        return tlOperation;
    }

    @Override
    public List getSelectList(JSONObject params) {
        Map<String, Object> map = new HashMap();
        map.put("type", params.getString("type"));
        List<Map> resultList = tlOperationMapper.getSelectList(map);
        for (Map resmap : resultList) {
            Map itemmap = new HashMap();
            itemmap.put("opeid", resmap.get("id"));
            List itemlist = tlOperationItemMapper.getSelectList(itemmap);
            resmap.put("itemlist", itemlist);
        }
        return resultList;
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("operationtype", params.getString("operationtype"));
        map.put("type", params.getString("type"));
        map.put("status", params.getString("status"));
        Page<TlOperation> listPage = new Page<>(page, size, true);
        IPage<TlOperation> iPage = tlOperationMapper.listTlOperation(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("resultlist", iPage.getRecords());
        return json;
    }
    @Override
    public Map<String, TlOperation> getTlOperationMap(Wrapper<TlOperation> tlOperationQueryWrapper)  {
        List<TlOperation> tlOperationList=tlOperationMapper.selectList(tlOperationQueryWrapper);
        Map<String, TlOperation> tlOperationMap= new HashMap<>();
        if (!tlOperationList.isEmpty()) {
            tlOperationMap = GroupingByUtils.getGroupingForMapEntity(tlOperationList, TlOperation::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        return tlOperationMap;
    }
    @Override
    public List<TlOperation> selectList(Wrapper<TlOperation> tlOperationQueryWrapper)  {
        List<TlOperation> tlOperationList=tlOperationMapper.selectList(tlOperationQueryWrapper);
        return tlOperationList;
    }
    @Override
    public  TlOperation selectOne(String id)  {
        QueryWrapper<TlOperation> tlOperationQueryWrapper = new QueryWrapper<>();
        tlOperationQueryWrapper.lambda().eq(TlOperation::getId,id);
        TlOperation tlOperation=tlOperationMapper.selectOne(tlOperationQueryWrapper);
        return tlOperation;
    }
}
